using Mono.Xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security;

public class XMLParser
{
	public Dictionary<string, Dictionary<string, string>> LoadMap(string fileName, out string key)
	{
		key = Path.GetFileNameWithoutExtension(fileName);
		SecurityElement xml = Load(fileName);
		return LoadMap(xml);
	}

	public bool LoadMap(string fileName, out Dictionary<string, Dictionary<string, string>> map)
	{
		try
		{
			SecurityElement xml = Load(fileName);
			map = LoadMap(xml);
			return true;
		}
		catch (Exception ex)
		{
			LoggerHelper.Except(ex);
			map = null;
			return false;
		}
	}

	public static bool LoadIntMap(string fileName, bool isForceOutterRecoure, out Dictionary<int, Dictionary<string, string>> map)
	{
		try
		{
			SecurityElement securityElement = (!isForceOutterRecoure) ? Load(fileName) : LoadOutter(fileName);
			if (securityElement == null)
			{
				LoggerHelper.Error("File not exist: " + fileName);
				map = null;
				return false;
			}
			map = LoadIntMap(securityElement, fileName);
			return true;
		}
		catch (Exception ex)
		{
			LoggerHelper.Error("Load Int Map Error: " + fileName + "  " + ex.Message);
			map = null;
			return false;
		}
	}

	public static Dictionary<int, Dictionary<string, string>> LoadIntMap(SecurityElement xml, string source)
	{
		Dictionary<int, Dictionary<string, string>> dictionary = new Dictionary<int, Dictionary<string, string>>();
		int num = 0;
		IEnumerator enumerator = xml.Children.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				SecurityElement securityElement = (SecurityElement)enumerator.Current;
				num++;
				if (securityElement.Children == null || securityElement.Children.Count == 0)
				{
					LoggerHelper.Warning("empty row in row NO." + num + " of " + source);
				}
				else
				{
					int num2 = int.Parse((securityElement.Children[0] as SecurityElement).Text);
					if (dictionary.ContainsKey(num2))
					{
						LoggerHelper.Warning($"Key {num2} already exist, in {source}.");
					}
					else
					{
						Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
						dictionary.Add(num2, dictionary2);
						for (int i = 1; i < securityElement.Children.Count; i++)
						{
							SecurityElement securityElement2 = securityElement.Children[i] as SecurityElement;
							string key;
							if (securityElement2.Tag.Length < 3)
							{
								key = securityElement2.Tag;
							}
							else
							{
								switch (securityElement2.Tag.Substring(securityElement2.Tag.Length - 2, 2))
								{
								case "_i":
								case "_s":
								case "_f":
								case "_l":
								case "_k":
								case "_m":
									key = securityElement2.Tag.Substring(0, securityElement2.Tag.Length - 2);
									break;
								default:
									key = securityElement2.Tag;
									break;
								}
							}
							if (securityElement2 != null && !dictionary2.ContainsKey(key))
							{
								if (string.IsNullOrEmpty(securityElement2.Text))
								{
									dictionary2.Add(key, string.Empty);
								}
								else
								{
									dictionary2.Add(key, securityElement2.Text.Trim());
								}
							}
							else
							{
								LoggerHelper.Warning($"Key {securityElement2.Tag} already exist, index {i} of {securityElement2.ToString()}.");
							}
						}
					}
				}
			}
			return dictionary;
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	public static Dictionary<string, Dictionary<string, string>> LoadMap(SecurityElement xml)
	{
		Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
		IEnumerator enumerator = xml.Children.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				SecurityElement securityElement = (SecurityElement)enumerator.Current;
				string text = (securityElement.Children[0] as SecurityElement).Text.Trim();
				if (dictionary.ContainsKey(text))
				{
					LoggerHelper.Warning($"Key {text} already exist, in {xml.ToString()}.");
				}
				else
				{
					Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
					dictionary.Add(text, dictionary2);
					for (int i = 1; i < securityElement.Children.Count; i++)
					{
						SecurityElement securityElement2 = securityElement.Children[i] as SecurityElement;
						if (securityElement2 != null && !dictionary2.ContainsKey(securityElement2.Tag))
						{
							if (string.IsNullOrEmpty(securityElement2.Text))
							{
								dictionary2.Add(securityElement2.Tag, string.Empty);
							}
							else
							{
								dictionary2.Add(securityElement2.Tag, securityElement2.Text.Trim());
							}
						}
						else
						{
							LoggerHelper.Warning($"Key {securityElement2.Tag} already exist, index {i} of {securityElement2.ToString()}.");
						}
					}
				}
			}
			return dictionary;
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	public static string LoadText(string fileName)
	{
		try
		{
			return Utils.LoadResource(fileName);
		}
		catch (Exception ex)
		{
			LoggerHelper.Except(ex);
			return string.Empty;
		}
	}

	public static SecurityElement Load(string fileName)
	{
		string text = LoadText(fileName);
		if (string.IsNullOrEmpty(text))
		{
			return null;
		}
		return LoadXML(text);
	}

	public static SecurityElement LoadOutter(string fileName)
	{
		string text = Utils.LoadFile(fileName.Replace('\\', '/'));
		if (string.IsNullOrEmpty(text))
		{
			return null;
		}
		return LoadXML(text);
	}

	public static SecurityElement LoadXML(string xml)
	{
		try
		{
			SecurityParser securityParser = new SecurityParser();
			securityParser.LoadXml(xml);
			return securityParser.ToXml();
		}
		catch (Exception ex)
		{
			LoggerHelper.Except(ex);
			return null;
		}
	}

	public static void SaveBytes(string fileName, byte[] buffer)
	{
		if (!Directory.Exists(Utils.GetDirectoryName(fileName)))
		{
			Directory.CreateDirectory(Utils.GetDirectoryName(fileName));
		}
		if (File.Exists(fileName))
		{
			File.Delete(fileName);
		}
		using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
		{
			using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
			{
				binaryWriter.Write(buffer);
				binaryWriter.Flush();
				binaryWriter.Close();
			}
			fileStream.Close();
		}
	}

	public static void SaveText(string fileName, string text)
	{
		if (!Directory.Exists(Utils.GetDirectoryName(fileName)))
		{
			Directory.CreateDirectory(Utils.GetDirectoryName(fileName));
		}
		if (File.Exists(fileName))
		{
			File.Delete(fileName);
		}
		using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
		{
			using (StreamWriter streamWriter = new StreamWriter(fileStream))
			{
				streamWriter.Write(text);
				streamWriter.Flush();
				streamWriter.Close();
			}
			fileStream.Close();
		}
	}
}
